package com.tianshuo.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Aspect
@Component
public class LogAspectJ {

    private static final Logger log = LoggerFactory.getLogger(LogAspectJ.class);
    /**
     * 抽取公共的切入点表达式
     * 1。 本类中直接引用:pointCut()
     * 2。其他切面引用:public String com.tina.spring0123.aop.Star.*(..)
     */
    @Pointcut("execution(public * *(..))")
    public void pointCut(){};


    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        //获取被调用的方法的方法名
        String name = joinPoint.getSignature().getName();
        //获取被调用的方法的参数
        Object[] args = joinPoint.getArgs();
        log.info("调用{}方法之前,其参数为：{}",name,Arrays.asList(args));
    }

    @After("pointCut()")
    public void after(){
        log.info("调用方法之后");
    }


    @AfterReturning(value = "pointCut()", returning = "response")
    public void afterReturn(JoinPoint joinPoint,Object response) {
        log.info("方法返回之后: {}",response);
    }

    @AfterThrowing(throwing = "e",value = "pointCut()")
    public void afterThrowing(Throwable e) {
        log.info("异常抛出:{}",e);
    }

}
